package com.itheima.controller;

import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.service.StudentsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentsController {

    @Autowired
    private StudentsService studentsService;

    /**
     * 分页查询
     */
    @GetMapping
    public Result page(String name, Integer degree, Integer clazzId,
                       @RequestParam(defaultValue = "1") Integer page ,
                       @RequestParam(defaultValue = "10")Integer pageSize) {
        PageResult pageResult = studentsService.page(name , degree , clazzId , page , pageSize);
        return Result.success(pageResult);
    }

    /**
     * 根据id 查询学生信息
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") Integer id) {
        log.info("根据id查询学生信息: {}" , id);
        Student student = studentsService.getInfo(id);
        return Result.success(student);
    }

    /**
     * 修改学生信息
     */
    @PutMapping()
    public Result update(@RequestBody Student student) {
        log.info("修改学生信息: {}" , student);
        studentsService.update(student);
        return Result.success();
    }

    /**
     * 违纪扣分
     */
    @PutMapping("/violation/{id}/{score}")
    public Result deduct(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("违纪扣分: {}" , id);
        studentsService.deduct(id, score);
        return Result.success();
    }

    /**
     * 添加学员
     */
    @PostMapping()
    public Result add(@RequestBody Student student) {
        log.info("添加学员: {}" , student);
        studentsService.add(student);
        return Result.success();
    }

    /**
     * 删除学员
     */

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除学员: {}" , ids);
        studentsService.delete(ids);
        return Result.success();
    }
}
